//
//  ClassifyViewController.m
//  美食百科
//
//  Created by dllo on 16/1/23.
//  Copyright © 2016年 doll. All rights reserved.
//

#import "ClassifyViewController.h"
#import "NewWorking.h"
#import "ClassModel.h"
#import "ClassifyCollectionViewCell.h"
#import "StapleViewController.h"
#import "Reachability.h"
#define kWidth self.view.frame.size.width
#define kHeight self.view.frame.size.height
@interface ClassifyViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property(nonatomic,retain)UICollectionView *classView;

@property(nonatomic,retain)NSMutableArray *classArray;
@property(nonatomic,retain)NSMutableArray *classChooseArray;


@end

@implementation ClassifyViewController
- (void)dealloc
{  [_kindString release];
    [_itemString release];
    [_classArray release];
    [_classView release];
    [_classChooseArray release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //self.navigationController.navigationBarHidden = NO;
   
   // [self prefersStatusBarHidden];
    self.navigationController.navigationBar.tintColor = [UIColor grayColor];
    self.navigationController.navigationBarHidden = NO;
    self.navigationController.navigationBar.translucent = NO;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.9 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        Reachability *reach = [Reachability reachabilityForInternetConnection];
        
        //如果无网络,弹窗提示
        if ( 0 == reach.currentReachabilityStatus){
            //弹窗提示
            UILabel *nonconnectionLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, -50, kWidth, 50)];
            nonconnectionLabel.textAlignment = NSTextAlignmentCenter;
            nonconnectionLabel.backgroundColor = [UIColor redColor];
            nonconnectionLabel.text = @"网络不给力,刷新试试~";
            nonconnectionLabel.textColor = [UIColor whiteColor];
            [self.view addSubview:nonconnectionLabel];
            
            //动画效果,通知栏缓慢出现
            [UIView animateWithDuration:0.6 animations:^{
                nonconnectionLabel.frame = CGRectMake(0, 0, kWidth, 50);
            } completion:^(BOOL finished) {
                [UIView animateWithDuration:0.6 delay:3 options:UIViewAnimationOptionAutoreverse animations:^{
                    nonconnectionLabel.frame = CGRectMake(0, -50, kWidth, 50);
                    [nonconnectionLabel release];
                    
                } completion:^(BOOL finished) {
                    
                }];
                
                //            [UIView animateWithDuration:1.9 animations:^{
                //                nonconnectionLabel.frame = CGRectMake(0, -50, kWidth, 50);
                //            //[nonconnectionLabel removeFromSuperview];
                //                [nonconnectionLabel release];
                //            }];
                
            }];
            
        }
    });

    UIBarButtonItem *leftItemFirst = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"iconfont-zuojiantou-3"] style:UIBarButtonItemStylePlain target:self action:@selector(backAction:)];

    UIBarButtonItem *leftItemSecond = [[UIBarButtonItem alloc]initWithTitle:self.itemString style:UIBarButtonItemStyleDone target:self action:nil];
    self.navigationItem.leftBarButtonItems = @[leftItemFirst,leftItemSecond];

    self.classArray = [NSMutableArray array];
    self.classChooseArray = [NSMutableArray array];

    
    [NewWorking GetDayaWithURL:[NSString stringWithFormat:@"http://food.boohee.com/fb/v1/categories?kind=%@",self.kindString ] dic:nil sussess:^(id responseObject) {
                
        for (NSDictionary *dictionary in responseObject[@"categories"]) {
            ClassModel *class = [[ClassModel alloc]init];
            [class setValuesForKeysWithDictionary:dictionary];
            
            [self.classArray addObject:class];
            [class release];
            NSMutableArray *temp = [NSMutableArray array];
            for (NSDictionary *dic in dictionary[@"sub_categories"]) {
                ClassModel *classChoose = [[ClassModel alloc]init];
                [classChoose setValuesForKeysWithDictionary:dic];
                
                [temp addObject:classChoose];
               // NSLog(@"🌲%ld",self.classChooseArray.count);
                [classChoose release];
            
        }
            [self.classChooseArray addObject:temp];

      }
            
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake(kWidth/3, (kHeight)/5);
   // NSLog(@"%.2f,%.2f",(kHeight-64)/5*4/4,layout.itemSize.height);
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    self.classView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight) collectionViewLayout:layout];
    self.classView.backgroundColor = [UIColor clearColor];
    self.classView.dataSource = self;
    self.classView.delegate  = self;
        self.classView.bounces = NO;
        self.classView.showsVerticalScrollIndicator = NO;
   // self.classView.scrollEnabled = NO;
    [self.view addSubview:self.classView];
        [layout release];
    [_classView release];
    
    [self.classView registerClass:[ClassifyCollectionViewCell class] forCellWithReuseIdentifier:@"classCell"];
            
                    
        
    } filed:^(NSError *error) {
        NSLog(@"%@",error);
    }];
    
   
        
    
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.classArray.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ClassifyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"classCell" forIndexPath:indexPath];
    cell.classModel = self.classArray[indexPath.row];
   // cell.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1];
    return cell;
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    //跳转列表界面
    StapleViewController *staple = [[StapleViewController alloc]init];
    staple.classModel = self.classArray[indexPath.row];
    staple.kindStr = self.kindString;
    NSLog(@"🌲%@",staple.kindStr);
    staple.array = self.classChooseArray[indexPath.row];
    NSLog(@"🐌%ld",[self.classChooseArray[indexPath.row] count]);
            [self.navigationController pushViewController:staple animated:YES];
            [staple release];
    

    
    
    
}



//-(void)viewDidAppear:(BOOL)animated
//{
//    [self.classView reloadData];
//}

-(void)backAction:(UIBarButtonItem *)item{
    [self.navigationController popViewControllerAnimated:YES];
    self.navigationController.navigationBarHidden = YES;
}

//-(void)viewDidAppear:(BOOL)animated
//{
//    NSLog(@"kkk");
//    [self prefersStatusBarHidden];
//    NSLog(@"hhh");
//}
 - (BOOL)prefersStatusBarHidden{
     NSLog(@"ne");
    return NO;
   
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
